package com.ques.dao;

import com.mongodb.client.result.DeleteResult;
import com.mongodb.client.result.UpdateResult;
import com.ques.entity.QueryParam;
import com.ques.pojo.Question;
import com.ques.pojo.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * @author zq  2020-01-05
 */
@Repository
public class QuestionDao {
    @Autowired
    private MongoTemplate mongoTemplate;

    public void item() {
        Query id = Query.query(Criteria.where("_id").is("5e11c33eecef2d1b0603e723"));
        List<User> users = mongoTemplate.find(id, User.class);
        System.out.println(users);
    }

    public Question insertQuestionnaire(Question question) {
        Question ques = mongoTemplate.insert(question);
        return ques;
    }

    @SuppressWarnings("all")
    public List<Question> selectAllQuestion(QueryParam queryParam) {
        Query query = new Query();

        // 重第0 也开始算的
        // 计算数量够不够
//        if ( (queryParam.getCurrentPage()+1) * queryParam.getPageSize() > total) {
//            queryParam.setCurrentPage( (int)total / queryParam.getPageSize() );
//        }

        query.addCriteria(Criteria.where("userId").is(queryParam.getId())) ;
        long total = mongoTemplate.count(query, "question");
        queryParam.setTotal(total);

        Pageable pageable = PageRequest.of(queryParam.getCurrentPage(), queryParam.getPageSize());
        List<Question> questions = mongoTemplate.find(query.with(pageable),  Question.class,"question");
        return questions ;
    }


    public void updateQuestion(Question question) {
        Question byId = mongoTemplate.findById(question.getId(), Question.class);
        mongoTemplate.remove(byId) ;
        Question insert = mongoTemplate.insert(question);
        System.out.println(insert);
    }

    public Question selectQuestionById(String page) {
       return mongoTemplate.findById(page,Question.class);
    }

    public boolean updateQuestionStatus(String questionId, boolean status) {
        Query query = Query.query(Criteria.where("_id").is(questionId));
        Update update = Update.update("status", status);

        UpdateResult result = mongoTemplate.updateFirst(query, update, Question.class);
        long modifiedCount = result.getModifiedCount();
        return modifiedCount == 1 ;
    }

    public long deleteQuestion(String[] questionIds) {
        Query query = new Query();

        for (String questionId : questionIds) {
            query.addCriteria(Criteria.where("_id").is(questionId)) ;
        }

        DeleteResult remove = mongoTemplate.remove(query, Question.class);
        return remove.getDeletedCount() ;
    }
}
